//
//  NavigationController.m
//  SmallElephant
//
//  Created by Faith on 2024/11/11.
//

#import "NavigationController.h"
#import "../User/Login/LoginController.h"

@interface NavigationController ()


@end

@implementation NavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self.view applyStyle:@"display: flex; flex-direction: column; background: #F5F5F5;"];
    [self.view addSubview:self.navigationBar style:@"display: flex; flex-direction: column;"];
    
    // 创建并配置点击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    // 允许点击事件继续传递给其他控件
    tapGesture.cancelsTouchesInView = NO;
    // 将手势添加到主视图上
    [self.view addGestureRecognizer:tapGesture];
    
}

// 手势的回调方法，用于关闭键盘
- (void)dismissKeyboard {
    [self.view endEditing:YES];
}

- (UIView *)navigationBar {
    if(!_navigationBar) {
        _navigationBar = [[UIView alloc] init];
        
        // 状态栏占位控件
        UIView *statusBarPlaceHolder = [[UIView alloc] init];
        [_navigationBar addSubview:statusBarPlaceHolder style:@"display: flex; height: --status-bar-height;"];
        
        // 状态栏内容容器
        UIView *statusBarContent = [[UIView alloc] init];
        [_navigationBar addSubview:statusBarContent style:@"display: flex; flex-direction: row;"];
        
        // 左侧视图
        _navigationBarLeftView = [[UIView alloc] init];
        [statusBarContent addSubview:_navigationBarLeftView style:@"display: flex;"];
        // 左侧按钮默认返回
        _btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnLeft setImage:[UIImage systemImageNamed:@"chevron.left"] forState:UIControlStateNormal];
        [_btnLeft addTarget:self action:@selector(btnLeftClickEventHandler) forControlEvents:UIControlEventTouchUpInside];
        _btnLeft.tintColor = [UIColor colorWithHexString:@"#333333"];
        [_navigationBarLeftView addSubview:_btnLeft style:@"display: flex; width: 40px; height: 40px;"];
        
        // 中间视图
        _navigationBarMiddleView = [[UIView alloc] init];
        [statusBarContent addSubview:_navigationBarMiddleView style:@"display: flex; flex-direction: column; justify-content: center; flex: 1;"];
        // 标题视图
        _titleView = [[UILabel alloc] init];
        _titleView.font = [UIFont systemFontOfSize:FONT_SIZE(16) weight:UIFontWeightBold];
        [_navigationBarMiddleView addSubview:_titleView style:@"display: flex; align-self: center; color: #333333;"];
        
        //右侧视图
        _navigationBarRightView = [[UIView alloc] init];
        [statusBarContent addSubview:_navigationBarRightView style:@"display: flex; width: 40px;"];
    }
    return _navigationBar;
}

/// 设置标题
- (void)setTitleText:(NSString*)title {
    self.titleView.text = title;
    [self.titleView applyStyle];
}

/// 设置自定义标题视图
- (void)setCustomTitleView:(UIView*)view style:(NSString*)style {
    [self.titleView removeFromSuperview];
    [self.navigationBarMiddleView addSubview:view style:style];
}

/// 左边按钮点击事件
- (void)btnLeftClickEventHandler {
    NSUInteger count = self.navigationController.viewControllers.count;
    if (count==1) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
        return;
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/// 页面跳转
- (void)pushViewController:(UIViewController*)viewController {
    [self pushViewController:viewController fromTabbar:NO checkPermission:NO];
}

/// 页面跳转
- (void)pushViewController:(UIViewController*)viewController fromTabbar:(BOOL)fromTabbar {
    [self pushViewController:viewController fromTabbar:fromTabbar checkPermission:NO];
}

/// 页面跳转
- (void)pushViewController:(UIViewController*)viewController checkPermission:(BOOL)checkPermission {
    [self pushViewController:viewController fromTabbar:NO checkPermission:checkPermission];
}

/// 页面跳转
- (void)pushViewController:(UIViewController*)viewController fromTabbar:(BOOL)fromTabbar checkPermission:(BOOL)checkPermission {
    if(checkPermission && ![Utils isLogin]) {
        LoginController *loginController = [[LoginController alloc] init];
        UINavigationController *loginNavController = [[UINavigationController alloc] initWithRootViewController:loginController];
        loginNavController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.navigationController presentViewController:loginNavController animated:YES completion:nil];
        return;
    }
    [self.navigationController pushViewController:viewController animated:YES];
}

/// 返回按钮点击事件
- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

/// 返回顶部控制器
- (void)popToRootViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.view configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
        layout.isEnabled = YES;
    }];
    [self.view.yoga applyLayoutPreservingOrigin:YES];
}


@end
